import appService from '@/utils/appService';
import systemConfig from '@/utils/config';
import util from '@/utils/util';

const baseUrl = `${systemConfig.serverProxy}`;

/**
 * @description 获取收入成本汇总数据
 * @see https://www.tapd.com/wiki/xxxx
 * @param {*} params
 */
export const getCostSummary = async params => {
    const url = `${baseUrl}/trade-settlement/admin/revenue/summary`;
    const res = await appService.getRequest(url, params);
    return res;
};

/**
 * @description 获取系统来源列表
 * @param {*} params
 */
export const getAllSystemSource = async params => {
    const url = `${baseUrl}/trade-settlement/admin/revenue/allSystemSource`;
    const res = await appService.getRequest(url, params);
    return res.data.data || [];
};

/**
 * @description 获取收入成本汇总-明细
 * @see https://www.tapd.com/wiki/xxxx
 * @param {*} params
 */
export const getCostSummaryDetail = async params => {
    const url = `${baseUrl}/trade-settlement/admin/revenue/detail`;
    const res = await appService.getRequest(url, params);
    return res;
};
/**
 * @description 获取对账结果列表
 * @see https://www.tapd.com/wiki/xxxx
 * @param {*} params
 */
export const getSettleSummaryList = async params => {
    const url = `${baseUrl}/trade-settlement/admin/settleSummary/list`;
    const res = await appService.postRequest(url, params);
    return res;
};

/**
 * 导出收入汇总
 * @param params
 * @returns {Promise<unknown>}
 */
export const exportRevenueSummary = async params => {
    const res = await appService.getRequest(`${baseUrl}/trade-settlement/admin/revenue/summary/export`, params, { responseType: 'arraybuffer' });
    return new Promise((resolve, reject) => {
        resolve(res);
    });
};

/**
 * 导出收入明细
 * @param params
 * @returns {Promise<unknown>}
 */
export const exportRevenueDetail = async params => {
    return new Promise((resolve, reject) => {
        appService
            .getRequest(
                `${baseUrl}/trade-settlement/admin/revenue/detail/export`,
                params, { headers: { 'Content-type': 'application/x-www-form-urlencoded ' }, responseType: 'arraybuffer' }
            )
            .then(result => {
                if (result && result.status === 200) {
                    resolve(result);
                } else {
                    reject(Error);
                }
            });
    });
};
/**
 * 查询对账函列表
 * @returns {Promise<void>}
 */
export const getSettleLetters = async params => {
    const res = await appService.postRequest(`${baseUrl}/trade-settlement/admin/settleLetters`, params);
    return res;
};

/**
 * 生成对账函
 * @returns {Promise<void>}
 */
export const generateSettleLetters = async params => {
    const res = await appService.postRequest(`${baseUrl}/trade-settlement/admin/settleLetters/generate`, params);
    return res;
};

/**
 * 删除对账函
 * @returns {Promise<void>}
 */
export const deleteBySettleLettersCode = async params => {
    const res = await appService.postRequest(`${baseUrl}/trade-settlement/admin/settleLetters/deleteBySettleLettersCode`, params);
    return res;
};

/**
 * 判断是否有任务在运行
 * @returns {Promise<void>}
 */
export const isExistGenerateTask = async() => {
    const res = await appService.getRequest(`${baseUrl}/trade-settlement/admin/settleLetters/isExistGenerateTask`, {});
    return res;
};

/**
 * 订单数据汇总统计
 * @returns {Promise<void>}
 */
export const serviceAmountCount = async params => {
    const res = await appService.getRequest(`${baseUrl}/trade-settlement/admin/settle/subOrder/serviceAmountCount`, params);
    return res;
};

/**
 * 订单数据汇总统计
 * @returns {Promise<void>}
 */
export const serviceDetailCountPage = async params => {
    const res = await appService.getRequest(`${baseUrl}/trade-settlement/admin/settle/subOrder/serviceDetailCountPage`, params);
    return res;
};

/**
 * 根据对账函明细列表
 * @returns {Promise<void>}
 */
export const querySettleLettersDetailList = async params => {
    const res = await appService.postRequest(`${baseUrl}/trade-settlement/admin/settleLettersDetail`, params);
    return res;
};

/**
 * 根据对账编号查询商户对账函明细
 * @returns {Promise<void>}
 */
export const byParentSettleLetter = async params => {
    const res = await appService.getRequest(`${baseUrl}/trade-settlement/admin/settleLettersDetail/byParentSettleLetter/${params}`);
    return res;
};

/**
 * 根据商户对账函编号查询商户对账函明细
 * @returns {Promise<void>}
 */
export const queryAdjustBySubSettleLettersCode = async params => {
    const res = await appService.getRequest(`${baseUrl}/trade-settlement/admin/settleLettersDetail/queryAdjustBySubSettleLettersCode/${params}`);
    return res;
};

/**
 * 提交手动调整单
 * @returns {Promise<void>}
 */
export const adjust = async params => {
    const res = await appService.postRequest(`${baseUrl}/trade-settlement/admin/settleLettersDetail/adjust`, params);
    return new Promise((resolve, reject) => {
        resolve(res);
    });
};

/**
 * 查询收入确认类型列表
 * @returns {Promise<void>}
 */
export const getRevenueType = async params => {
    const res = await appService.getRequest(`${baseUrl}/trade-merchant/admin/merchant/allRevenueType`, params);
    console.log(res);
    return res.data.data || [];
};

/**
 * 查询订单中心枚举
 * @returns {Promise<void>}
 */
export const getOrderDictionary = async type => {
    const res = await appService.getRequest(`${baseUrl}/trade-order/admin/property/dictionary`, { type });
    return res.data.data || [];
};

/**
 * 查询商户中心-所有商户
 * @returns {Promise<void>}
 */
export const getMerchantAllMerchant = async type => {
    const res = await appService.getRequest(`${baseUrl}/trade-merchant/admin/merchant/findOnlyMerchantList`, {});
    return res.data.data || [];
};

/**
 * 查询商户中心-所有机构
 * @returns {Promise<void>}
 */
export const getMerchantAllOrg = async type => {
    const res = await appService.getRequest(`${baseUrl}/trade-merchant/admin/organization/all`, {});
    return res.data.data || [];
};

/**
 * 商户清分规则管理-列表查询
 * @returns {Promise<void>}
 */
export const queryMerchantSettleRuleList = async payload => {
    const res = await appService.getRequest(`${baseUrl}/trade-merchant/admin/merchantSettleRule/`, payload);

    return res;
};

/**
 * 商户清分规则管理-新增
 * @returns {Promise<void>}
 */
export const submitMerchantSettleRule = async payload => {
    const res = await appService.postRequest(`${baseUrl}/trade-merchant/admin/merchantSettleRule/`, payload);
    return res;
};

/**
 * 商户清分规则管理-one
 * @returns {Promise<void>}
 */
export const queryMerchantSettleRuleOne = async payload => {
    const res = await appService.postRequest(`${baseUrl}/trade-merchant/admin/merchantSettleRule/${payload}`);
    return res;
};

/**
 * 文件下载
 * @returns {Promise<void>}
 */
export const downLoadFile = async(url, name) => {
    const userInfo = await appService.getUserInfo();
    const res = await appService.getRequest(url, { jwt: userInfo.jwt });
    if (res) {
        util.exportExcle(res, name, 'zip');
    }
};

/**
 * 文件下载
 * @returns {Promise<void>}
 */
export const exportSettleLetters = async payload => {
    const res = await appService.postRequest(`${baseUrl}/trade-settlement/admin/settleLettersDetail/export`, payload, { responseType: 'arraybuffer' });
    return res;
};

/**
 * 订单统计明细导出
 * @returns {Promise<void>}
 */
export const serviceDetailCountExport = async payload => {
    const res = await appService.getRequest(`${baseUrl}/trade-settlement/admin/settle/subOrder/serviceDetailCountExport`, payload, { responseType: 'arraybuffer' });
    return res;
};

/**
 * 获取上传文件地址
 * @returns {Promise<void>}
 */
export const getUploadPath = `${baseUrl}/trade-settlement/admin/common/upload`;

/**
 * 订单统计明细导出
 * @returns {Promise<void>}
 */
export const uploadAdjustByOriginalLettersCode = async payload => {
    const res = await appService.postRequest(`${baseUrl}/trade-settlement/admin/settleLettersDetail/uploadAdjustByOriginalLettersCode`, payload);

    return res;
};

/**
 * 查询优惠券账单
 * @returns {Promise<void>}
 */
export const getCouponBillList = async params => {
    const res = await appService.postRequest(`${baseUrl}/trade-settlement/admin/couponBill/query`, params);
    return res;
};

/**
 * 导出优惠券账单
 * @returns {Promise<void>}
 */
export const exportCouponBillList = async payload => {
    const res = await appService.postRequest(`${baseUrl}/trade-settlement/admin/couponBill/export`, payload, { responseType: 'arraybuffer' });
    return res;
};